{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Ch `02`: Concept `08`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using TensorBoard"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "TensorBoard is a great way to visualize what's happening behind the code. \n",
    "\n",
    "In this example, we'll loop through some numbers to improve our guess of the average value. Then we can visualize the results on TensorBoard. \n",
    "\n",
    "Let's just set ourselves up with some data to work with:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "\n",
    "raw_data = np.random.normal(10, 1, 100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The moving average is defined as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "alpha = tf.constant(0.05)\n",
    "curr_value = tf.placeholder(tf.float32)\n",
    "prev_avg = tf.Variable(0.)\n",
    "\n",
    "update_avg = alpha * curr_value + (1 - alpha) * prev_avg"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here's what we care to visualize:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "avg_hist = tf.summary.scalar(\"running_average\", update_avg)\n",
    "value_hist = tf.summary.scalar(\"incoming_values\", curr_value)\n",
    "\n",
    "merged = tf.summary.merge_all()\n",
    "writer = tf.summary.FileWriter(\"./logs\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Time to compute the moving averages. We'll also run the `merged` op to track how the values change:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9.247841477069203 0.4623921\n",
      "10.019298730125382 0.9402374\n",
      "11.971773672793464 1.4918143\n",
      "10.702923359431118 1.9523697\n",
      "11.667057068606786 2.4381042\n",
      "9.143228690197773 2.7733603\n",
      "9.457709656523708 3.1075776\n",
      "12.33999608545561 3.5691986\n",
      "9.543410229631846 3.8679092\n",
      "9.251442209932934 4.137086\n",
      "8.942198790212387 4.3773413\n",
      "11.019946553148321 4.709471\n",
      "11.430198193578404 5.0455074\n",
      "8.6213954795195 5.224302\n",
      "10.822108995258686 5.504192\n",
      "10.58310002901428 5.7581377\n",
      "10.20420365104725 5.9804406\n",
      "10.312154931419304 6.1970263\n",
      "10.545111153579882 6.4144306\n",
      "8.797765458370709 6.5335975\n",
      "8.56686695526782 6.6352606\n",
      "12.570525410195215 6.9320235\n",
      "11.543815331679966 7.162613\n",
      "10.320920832332627 7.320528\n",
      "10.423914230722215 7.4756975\n",
      "10.619258439210187 7.6328754\n",
      "9.101109809288653 7.7062874\n",
      "9.841278298991933 7.813037\n",
      "9.099955845561944 7.877383\n",
      "9.41973125623955 7.9545\n",
      "11.082836040691273 8.110917\n",
      "10.116690980009775 8.2112055\n",
      "9.402594289154155 8.270775\n",
      "10.925993106488145 8.403536\n",
      "10.243254438024696 8.495522\n",
      "9.477769687949733 8.544634\n",
      "9.351362392482848 8.58497\n",
      "9.242191906408548 8.617831\n",
      "12.123667719477677 8.793122\n",
      "10.076009517273803 8.857266\n",
      "9.74900667301667 8.901854\n",
      "10.830363231386094 8.998279\n",
      "8.861116004341559 8.991421\n",
      "10.007389057190906 9.042218\n",
      "10.769369554012615 9.128575\n",
      "12.971561516039255 9.3207245\n",
      "9.875042913748056 9.34844\n",
      "9.64616462712992 9.363326\n",
      "9.76634851758219 9.383477\n",
      "9.326634526001623 9.380634\n",
      "8.492294014699189 9.336217\n",
      "10.006073094467316 9.369709\n",
      "9.442892778881891 9.373368\n",
      "9.56787198816676 9.383093\n",
      "9.961494974707488 9.412013\n",
      "9.572285501643822 9.420026\n",
      "11.851354361154291 9.541592\n",
      "10.833573476171445 9.606191\n",
      "11.836376240592454 9.7177\n",
      "11.047626672901409 9.784197\n",
      "10.913818292308468 9.840677\n",
      "10.60857743486623 9.879072\n",
      "9.883074005285522 9.879272\n",
      "8.227633816367192 9.79669\n",
      "9.788906167639809 9.796301\n",
      "9.001469197788671 9.756559\n",
      "8.918205933440774 9.714642\n",
      "9.885320274459133 9.723175\n",
      "10.77521268535355 9.775776\n",
      "9.68349427673202 9.771162\n",
      "10.113753965038361 9.788292\n",
      "9.6597232190883 9.781863\n",
      "9.323572053812015 9.758949\n",
      "9.618532841629188 9.751928\n",
      "9.011944462852757 9.714929\n",
      "8.323719148832197 9.645369\n",
      "9.442883485401897 9.635244\n",
      "10.430287903497137 9.674997\n",
      "10.838671174170663 9.733181\n",
      "9.346134056876938 9.713829\n",
      "10.234079103904495 9.739841\n",
      "9.692786236742311 9.737489\n",
      "8.675916172925552 9.68441\n",
      "9.7006074487691 9.68522\n",
      "10.064675943184373 9.704192\n",
      "9.4021612098359 9.689091\n",
      "11.124410899430886 9.760857\n",
      "10.034575898474612 9.774543\n",
      "9.793431430576485 9.775487\n",
      "10.889420930759462 9.831183\n",
      "9.253518007206916 9.8023\n",
      "11.114827470151916 9.867927\n",
      "9.378996323459113 9.843479\n",
      "9.864306640072803 9.844521\n",
      "11.803316169037448 9.94246\n",
      "10.103049011008196 9.95049\n",
      "8.723187258083906 9.889125\n",
      "8.985505621881307 9.843944\n",
      "10.690261212066178 9.88626\n",
      "8.426969249944442 9.813295\n"
     ]
    }
   ],
   "source": [
    "init = tf.global_variables_initializer()\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    for i in range(len(raw_data)):\n",
    "        summary_str, curr_avg = sess.run([merged, update_avg], feed_dict={curr_value: raw_data[i]})\n",
    "        sess.run(tf.assign(prev_avg, curr_avg))\n",
    "        print(raw_data[i], curr_avg)\n",
    "        writer.add_summary(summary_str, i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Check out the visualization by running TensorBoard from the terminal:\n",
    "\n",
    "    $ tensorboard --logdir=path/to/logs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#made the logs be written successfully\n",
    "writer.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
